from rollDice import diceRoller
import wx

import riftsRace
import riftsClass
import riftsOptions
import pickle

masterClassList = riftsClass.getClassList() #dict
masterRaceList = riftsRace.getRaceList() #dict
options = riftsOptions.Options()

class Character:
    def __init__(self):
        self.Name = ""
        self.Race = ""
        self.Class = ""
        self.Alignment = ""
        self.IQ = ""
        # dict{key:[number,"dice (YES/NO)","diceRoll"]}
        self.IQdetails = {}
        self.ME = ""
        self.MEdetails = {}
        self.MA = ""
        self.MAdetails = {}
        self.PS = ""
        self.PSdetails = {}
        self.PP = ""
        self.PPdetails = {}
        self.PE = ""
        self.PEdetails = {}
        self.PB = ""
        self.PBdetails = {}
        self.SPD = ""
        self.SPDdetails = {}
        self.HP = ""
        self.HPdetails = {}
        self.SDC = ""
        self.SDCdetails = {}
        self.PPE = ""
        self.PPEdetails = {}
        self.ISP = ""
        self.ISPdetails = {}
        self.Desc = ""
        self.Descdetails = {}
        self.SaveVsSpellMagic = "12"
        self.SaveVsSpellMagicdetails = {}
        self.SaveVsRitualMagic = "16"
        self.SaveVsRitualMagicdetails = {}
        self.SaveVsPsionics = "15"
        self.SaveVsPsionicsdetails = {}
        self.SaveVsPoison = "14"
        self.SaveVsPoisondetails = {}
        self.SaveVsDrugs = "15"
        self.SaveVsDrugsdetails = {}
        self.SaveVsDisease = "14"
        self.SaveVsDiseasedetails = {}
        self.SaveVsInsanity = "12"
        self.SaveVsInsanitydetails = {}
        self.SaveVsMindControl = "+0"
        self.SaveVsMindControldetails = {}
        self.SaveVsHorrorFactor = "+0"
        self.SaveVsHorrorFactordetails = {}
        self.SaveVsIllusion = "+0"
        self.SaveVsIllusiondetails = {}
        self.SaveVsPossession = "+0"
        self.SaveVsPossessiondetails = {}
        self.SaveVsComa = "+0%"
        self.SaveVsComadetails = {}
        self.SaveVsPain = "+0"
        self.SaveVsPaindetails = {}
        self.SaveVsCurse = "15"
        self.SaveVsCursedetails = {}
        
    def debugIQadd(self):
        self.IQdetails.update({"Fake1":[5,"YES","2d6"]})
        self.IQdetails.update({"Fake2":[1,"NO",""]})
        self.IQdetails.update({"Fake3":[12,"NO",""]})
        self.IQdetails.update({"Fake4":[0,"YES","2d6"]})
        
    def debugIQcleanUp(self):
        del self.IQdetails["Fake1"]
        del self.IQdetails["Fake2"]
        del self.IQdetails["Fake3"]
        del self.IQdetails["Fake4"]
        
    def debugMEadd(self):
        self.MEdetails.update({"Fake1":[5,"YES","2d6"]})
        self.MEdetails.update({"Fake2":[1,"NO",""]})
        self.MEdetails.update({"Fake3":[12,"NO",""]})
        self.MEdetails.update({"Fake4":[0,"YES","2d6"]})
        
    def debugMEcleanUp(self):
        del self.MEdetails["Fake1"]
        del self.MEdetails["Fake2"]
        del self.MEdetails["Fake3"]
        del self.MEdetails["Fake4"]
        
    def debugMAadd(self):
        self.MAdetails.update({"Fake1":[5,"YES","2d6"]})
        self.MAdetails.update({"Fake2":[1,"NO",""]})
        self.MAdetails.update({"Fake3":[12,"NO",""]})
        self.MAdetails.update({"Fake4":[0,"YES","2d6"]})
        
    def debugMAcleanUp(self):
        del self.MAdetails["Fake1"]
        del self.MAdetails["Fake2"]
        del self.MAdetails["Fake3"]
        del self.MAdetails["Fake4"]
        
    def debugPSadd(self):
        self.PSdetails.update({"Fake1":[5,"YES","2d6"]})
        self.PSdetails.update({"Fake2":[1,"NO",""]})
        self.PSdetails.update({"Fake3":[12,"NO",""]})
        self.PSdetails.update({"Fake4":[0,"YES","2d6"]})
        
    def debugPScleanUp(self):
        del self.PSdetails["Fake1"]
        del self.PSdetails["Fake2"]
        del self.PSdetails["Fake3"]
        del self.PSdetails["Fake4"]
        
    def debugPPadd(self):
        self.PPdetails.update({"Fake1":[5,"YES","2d6"]})
        self.PPdetails.update({"Fake2":[1,"NO",""]})
        self.PPdetails.update({"Fake3":[12,"NO",""]})
        self.PPdetails.update({"Fake4":[0,"YES","2d6"]})
        
    def debugPPcleanUp(self):
        del self.PPdetails["Fake1"]
        del self.PPdetails["Fake2"]
        del self.PPdetails["Fake3"]
        del self.PPdetails["Fake4"]
        
    def debugPEadd(self):
        self.PEdetails.update({"Fake1":[5,"YES","2d6"]})
        self.PEdetails.update({"Fake2":[1,"NO",""]})
        self.PEdetails.update({"Fake3":[12,"NO",""]})
        self.PEdetails.update({"Fake4":[0,"YES","2d6"]})
        
    def debugPEcleanUp(self):
        del self.PEdetails["Fake1"]
        del self.PEdetails["Fake2"]
        del self.PEdetails["Fake3"]
        del self.PEdetails["Fake4"]
        
    def debugPBadd(self):
        self.PBdetails.update({"Fake1":[5,"YES","2d6"]})
        self.PBdetails.update({"Fake2":[1,"NO",""]})
        self.PBdetails.update({"Fake3":[12,"NO",""]})
        self.PBdetails.update({"Fake4":[0,"YES","2d6"]})
        
    def debugPBcleanUp(self):
        del self.PBdetails["Fake1"]
        del self.PBdetails["Fake2"]
        del self.PBdetails["Fake3"]
        del self.PBdetails["Fake4"]
        
    def debugSPDadd(self):
        self.SPDdetails.update({"Fake1":[5,"YES","2d6"]})
        self.SPDdetails.update({"Fake2":[1,"NO",""]})
        self.SPDdetails.update({"Fake3":[12,"NO",""]})
        self.SPDdetails.update({"Fake4":[0,"YES","2d6"]})
        
    def debugSPDcleanUp(self):
        del self.SPDdetails["Fake1"]
        del self.SPDdetails["Fake2"]
        del self.SPDdetails["Fake3"]
        del self.SPDdetails["Fake4"]

    def getClassList(self,classes):
        if self.Race == "":
            print "NO RACE"
            return classes
        if self.Race.Classes.getAttribute("type") == "NA" and self.Race.Classes.childNodes[0].data == "NONE":
            print "RCC"
            return classes[self.Race.Name]
        ClassList = classes.copy()
        del ClassList[""]
        raceLimitedClassList = self.Race.raceClassList(ClassList)
        finalClassList = {"":""}
        for Class in raceLimitedClassList.keys():
            if raceLimitedClassList[Class].isRaceAllowed(self.Race.Name):
                print "ADDED " + Class
                finalClassList.update({raceLimitedClassList[Class].Name:raceLimitedClassList[Class]})
        return finalClassList
        
    def getRaceList(self,races):
        if self.Class == "":
            print "NO CLASS"
            return races
        finalRaceList = self.Class.getRaceList(races)
        return finalRaceList
    
    def clearBaseStats(self):
        if "BASE" in self.IQdetails.keys():
            del self.IQdetails["BASE"]
            self.updateIQ()
        if "BASE" in self.MEdetails.keys():
            del self.MEdetails["BASE"]
            self.updateME()
        if "BASE" in self.MAdetails.keys():
            del self.MAdetails["BASE"]
            self.updateMA()
        if "BASE" in self.PSdetails.keys():
            del self.PSdetails["BASE"]
            self.updatePS()
        if "BASE" in self.PPdetails.keys():
            del self.PPdetails["BASE"]
            self.updatePP()
        if "BASE" in self.PEdetails.keys():
            del self.PEdetails["BASE"]
            self.updatePE()
        if "BASE" in self.PBdetails.keys():
            del self.PBdetails["BASE"]
            self.updatePB()
        if "BASE" in self.SPDdetails.keys():
            del self.SPDdetails["BASE"]
            self.updateSPD()
    
    def updateIQ(self):
        if self.IQdetails == {}:
            self.IQ = ""
        else:
            IQ = 0
            for bonus in self.IQdetails.keys():
                IQ += self.IQdetails[bonus][0]
            self.IQ = str(IQ)
        
    def setIQbase(self,score):
        if "BASE" in self.IQdetails.keys():
            del self.IQdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.IQ)
            self.IQdetails.update({"BASE":[roll[0],"YES",self.Race.IQ]})
        else:
            self.IQdetails.update({"BASE":[score,"YES",self.Race.IQ]})
        self.updateIQ()

    def setMEbase(self,score):
        if "BASE" in self.MEdetails.keys():
            del self.MEdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.ME)
            self.MEdetails.update({"BASE":[roll[0],"YES",self.Race.ME]})
        else:
            self.MEdetails.update({"BASE":[score,"YES",self.Race.ME]})
        self.updateME()

    def updateME(self):
        if self.MEdetails == {}:
            self.ME = ""
        else:
            ME = 0
            for bonus in self.MEdetails.keys():
                ME += self.MEdetails[bonus][0]
            self.ME = str(ME)
        
    def setMAbase(self,score):
        if "BASE" in self.MAdetails.keys():
            del self.MAdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.MA)
            self.MAdetails.update({"BASE":[roll[0],"YES",self.Race.MA]})
        else:
            self.MAdetails.update({"BASE":[score,"YES",self.Race.MA]})
        self.updateMA()

    def updateMA(self):
        if self.MAdetails == {}:
            self.MA = ""
        else:
            MA = 0
            for bonus in self.MAdetails.keys():
                MA += self.MAdetails[bonus][0]
            self.MA = str(MA)

    def updatePS(self):
        if self.PSdetails == {}:
            self.PS = ""
        else:
            PS = 0
            for bonus in self.PSdetails.keys():
                PS += self.PSdetails[bonus][0]
            self.PS = str(PS)
        
    def setPSbase(self,score):
        if "BASE" in self.PSdetails.keys():
            del self.PSdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.PS)
            self.PSdetails.update({"BASE":[roll[0],"YES",self.Race.PS]})
        else:
            self.PSdetails.update({"BASE":[score,"YES",self.Race.PS]})
        self.updatePS()

    def updatePP(self):
        if self.PPdetails == {}:
            self.PP = ""
        else:
            PP = 0
            for bonus in self.PPdetails.keys():
                PP += self.PPdetails[bonus][0]
            self.PP = str(PP)
        
    def setPPbase(self,score):
        if "BASE" in self.PPdetails.keys():
            del self.PPdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.PP)
            self.PPdetails.update({"BASE":[roll[0],"YES",self.Race.PP]})
        else:
            self.PPdetails.update({"BASE":[score,"YES",self.Race.PP]})
        self.updatePP()

    def updatePE(self):
        if self.PEdetails == {}:
            self.PE = ""
        else:
            PE = 0
            for bonus in self.PEdetails.keys():
                PE += self.PEdetails[bonus][0]
            self.PE = str(PE)
        
    def setPEbase(self,score):
        if "BASE" in self.PEdetails.keys():
            del self.PEdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.PE)
            self.PEdetails.update({"BASE":[roll[0],"YES",self.Race.PE]})
        else:
            self.PEdetails.update({"BASE":[score,"YES",self.Race.PE]})
        self.updatePE()

    def updatePB(self):
        if self.PBdetails == {}:
            self.PB = ""
        else:
            PB = 0
            for bonus in self.PBdetails.keys():
                PB += self.PBdetails[bonus][0]
            self.PB = str(PB)
        
    def setPBbase(self,score):
        if "BASE" in self.PBdetails.keys():
            del self.PBdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.PB)
            self.PBdetails.update({"BASE":[roll[0],"YES",self.Race.PB]})
        else:
            self.PBdetails.update({"BASE":[score,"YES",self.Race.PB]})
        self.updatePB()

    def updateSPD(self):
        if self.SPDdetails == {}:
            self.SPD = ""
        else:
            SPD = 0
            for bonus in self.SPDdetails.keys():
                SPD += self.SPDdetails[bonus][0]
            self.SPD = str(SPD)
        
    def setSPDbase(self,score):
        if "BASE" in self.SPDdetails.keys():
            del self.SPDdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.SPD)
            self.SPDdetails.update({"BASE":[roll[0],"YES",self.Race.SPD]})
        else:
            self.SPDdetails.update({"BASE":[score,"YES",self.Race.SPD]})
        self.updateSPD()
        
    def updateHP(self):
        if self.HPdetails == {}:
            self.HP = ""
        else:
            HP = 0
            for bonus in self.HPdetails.keys():
                HP += self.HPdetails[bonus][0]
            self.HP = str(HP)
        
    def setHPbase(self,score):
        if "BASE" in self.HPdetails.keys():
            del self.HPdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.HP)
            self.HPdetails.update({"BASE":[roll[0],"YES",self.Race.HP]})
        else:
            self.HPdetails.update({"BASE":[score,"YES",self.Race.HP]})
        self.updateHP()
        
    def updateSDC(self):
        if self.SDCdetails == {}:
            self.SDC = ""
        else:
            SDC = 0
            for bonus in self.SDCdetails.keys():
                SDC += self.SDCdetails[bonus][0]
            self.SDC = str(SDC)
        
    def setSDCbase(self,score):
        if "BASE" in self.SDCdetails.keys():
            del self.SDCdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.SDC)
            self.SDCdetails.update({"BASE":[roll[0],"YES",self.Race.SDC]})
        else:
            self.SDCdetails.update({"BASE":[score,"YES",self.Race.SDC]})
        self.updateSDC()
        
    def updateISP(self):
        if self.ISPdetails == {}:
            self.ISP = ""
        else:
            ISP = 0
            for bonus in self.ISPdetails.keys():
                ISP += self.ISPdetails[bonus][0]
            self.ISP = str(ISP)
        
    def setISPbase(self,score):
        if "BASE" in self.ISPdetails.keys():
            del self.ISPdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.ISP)
            self.ISPdetails.update({"BASE":[roll[0],"YES",self.Race.ISP]})
        else:
            self.ISPdetails.update({"BASE":[score,"YES",self.Race.ISP]})
        self.updateISP()
        
    def updatePPE(self):
        if self.PPEdetails == {}:
            self.PPE = ""
        else:
            PPE = 0
            for bonus in self.PPEdetails.keys():
                PPE += self.PPEdetails[bonus][0]
            self.PPE = str(PPE)
        
    def setPPEbase(self,score):
        if "BASE" in self.PPEdetails.keys():
            del self.PPEdetails["BASE"]
        if score == "ROLL":
            roll = self.statRoll(self.Race.PPE)
            self.PPEdetails.update({"BASE":[roll[0],"YES",self.Race.PPE]})
        else:
            self.PPEdetails.update({"BASE":[score,"YES",self.Race.PPE]})
        self.updatePPE()

    def statRoll(self,ROLL):
        reload(riftsOptions)
        options = riftsOptions.Options()
        if self.Race.Name == "Human":
            return self.humanRoll()
        return diceRoller(ROLL,options.CheatLvl)
    
    def humanRoll(self):
        reload(riftsOptions)
        options = riftsOptions.Options()
        roll = diceRoller("3d6",options.CheatLvl)
        if roll[0] < 16:
            return roll
        elif roll[0] > 15:
            rollOne = diceRoller("1d6",options.CheatLvl)
            if rollOne[0] < 6:
                roll2 = []
                roll2.append(roll[0] + rollOne[0])
                roll2.append(roll[1] + rollOne[1])
                return roll2
            elif rollOne[0] == 6:
                roll2 = []
                roll2.append(roll[0] + rollOne[0])
                roll2.append(roll[1] + rollOne[1])
                rollTwo = diceRoller("1d6",options.CheatLvl)
                roll3 = []
                roll3.append(roll2[0] + rollTwo[0])
                roll3.append(roll2[1] + rollTwo[1])
                return roll3
